/*******************************************

  TRABALHO DE CONCORRENTES - FINAL

    Roberty Manzini Bertolo - 7573399
    Cesar Milano - 7288939
    Caio André Rodrigues - 7656411

  Smooth - Sequencia

********************************************/

/*********************
Bibliotecas
*********************/
#include <stdio.h>
#include <stdlib.h>

/*********************
Definições
*********************/

/*********************
Funções gerais
*********************/

/******************
Função Principal
*******************/
int main (void){

	FILE *pfile;
	char firstLine[3];
	int lines, columns, maxPixel;
	int **R; // Matriz que estará armazenada a imagem - Cor R
	int **G; // Matriz que estará armazenada a imagem - Cor G
	int **B; // Matriz que estará armazenada a imagem - Cor B
	int **nR; // nova Matriz que estará armazenada a imagem - Cor R
	int **nG; // nova Matriz que estará armazenada a imagem - Cor G
	int **nB; // nova Matriz que estará armazenada a imagem - Cor B
	int i, j;

	/**********************************
	Leitura das informações iniciais
	**********************************/
	scanf("%s", firstLine);			// Le a primeira linha "P3"
	scanf("%d", &lines);			// Le a qntLinhas
	scanf("%d", &columns);			// Le a qntColunas
	scanf("%d", &maxPixel);			// Le o valor max do Pixels

	/**********************************
	Aloca as matrizes definindo um range de 2 pixel a mais como borda
	**********************************/
	R = (int **) malloc((lines+4)*sizeof(int *));
	G = (int **) malloc((lines+4)*sizeof(int *));
	B = (int **) malloc((lines+4)*sizeof(int *));
	nR = (int **) malloc((lines+4)*sizeof(int *));
	nG = (int **) malloc((lines+4)*sizeof(int *));
	nB = (int **) malloc((lines+4)*sizeof(int *));
	for(i=0;i<(lines+4);i++)
	{
		R[i] = (int *) malloc((columns+4)*sizeof(int));
		G[i] = (int *) malloc((columns+4)*sizeof(int));
		B[i] = (int *) malloc((columns+4)*sizeof(int));
		nR[i] = (int *) malloc((columns+4)*sizeof(int));
		nG[i] = (int *) malloc((columns+4)*sizeof(int));
		nB[i] = (int *) malloc((columns+4)*sizeof(int));
	}

	/**********************************
	Leitura dos valores da matriz e configura as bordas
	**********************************/
	for(i = 0; i<(lines+4); i++){
		for(j = 0; j < (columns+4); j++)
			if(i<=1 || i>=lines+2)
			{
				R[i][j]=0;
				G[i][j]=0;
				B[i][j]=0;
			}
			else if(j<=1 || j>=columns+2)
			{
				R[i][j]=0;
				G[i][j]=0;
				B[i][j]=0;
			} else
				scanf("%d %d %d ", &R[i][j], &G[i][j], &B[i][j]);
	}

	/**********************************
	Calculando Smooth 5x5
	**********************************/
	for(i = 2; i < (lines+2); i++){
		for(j = 2; j < (columns+2); j++){
				nR[i][j] =
					( R[i-2][j-2] + R[i-2][j-1] + R[i-2][j] + R[i-2][j+1] + R[i-2][j+2] +
					  R[i-1][j-2] + R[i-1][j-1] + R[i-1][j] + R[i-1][j+1] + R[i-1][j+2] +
					  R[i][j-2] + R[i][j-1] + R[i][j] + R[i][j+1] + R[i][j+2] +
					  R[i+1][j-2] + R[i+1][j-1] + R[i+1][j] + R[i+1][j+1] + R[i+1][j+2] +
					  R[i+2][j-2] + R[i+2][j-1] + R[i+2][j] + R[i+2][j+1] + R[i+2][j+2] ) / 25;

				nG[i][j] =
					( G[i-2][j-2] + G[i-2][j-1] + G[i-2][j] + G[i-2][j+1] + G[i-2][j+2] +
					  G[i-1][j-2] + G[i-1][j-1] + G[i-1][j] + G[i-1][j+1] + G[i-1][j+2] +
					  G[i][j-2] + G[i][j-1] + G[i][j] + G[i][j+1] + G[i][j+2] +
					  G[i+1][j-2] + G[i+1][j-1] + G[i+1][j] + G[i+1][j+1] + G[i+1][j+2] +
					  G[i+2][j-2] + G[i+2][j-1] + G[i+2][j] + G[i+2][j+1] + G[i+2][j+2] ) / 25;

				nB[i][j] =
					( B[i-2][j-2] + B[i-2][j-1] + B[i-2][j] + B[i-2][j+1] + B[i-2][j+2] +
					  B[i-1][j-2] + B[i-1][j-1] + B[i-1][j] + B[i-1][j+1] + B[i-1][j+2] +
					  B[i][j-2] + B[i][j-1] + B[i][j] + B[i][j+1] + B[i][j+2] +
					  B[i+1][j-2] + B[i+1][j-1] + B[i+1][j] + B[i+1][j+1] + B[i+1][j+2] +
					  B[i+2][j-2] + B[i+2][j-1] + B[i+2][j] + B[i+2][j+1] + B[i+2][j+2] ) / 25;
		}
	}

	/**********************************
	Salvando imagem no arquivo
	**********************************/
	pfile = fopen ("saida.ppm", "w");

	fprintf(pfile, "%s \n", firstLine);
	fprintf(pfile, "%d ", (lines));
	fprintf(pfile, "%d \n", (columns));
	fprintf(pfile, "%d \n", maxPixel);

	for(i = 2; i<lines+2;i++)
		for(j = 2; j<columns+2; j++)
			fprintf(pfile, "%d %d %d \n", nR[i][j], nG[i][j], nB[i][j]);

	/**********************************
	Liberando Matrizes
	**********************************/
	for(i=0;i<(lines+4);i++)
	{
		free(R[i]); free(G[i]); free(B[i]);
		free(nR[i]); free(nG[i]); free(nB[i]);
	}

	free(R); free(G); free(B);
	free(nR); free(nG); free(nB);

	return 0;
}
